# Hello World program in Python


class ElementsLengthError(TypeError):
    pass

class ArgumentsRangeError(TypeError):
    pass

class MyList:
    
    __elements = []
    
    def __init__(self, maxLength, count, elements = []):
        self.__max = maxLength
        self.__count = count
        if len(elements) != count:
            raise ElementsLengthError
        # 初始化__elements
        for i in range(maxLength):
            MyList.__elements.append(0)
        for i in range(len(elements)):
            MyList.__elements[i] = elements[i]
    
    def is_empty(self):
        if self.__count == 0:
            return True
        return False
    
    def is_full(self):
        return self.getMaxLength() == self.getCount()
    
    def length(self):
        return self.getCount()
    
    def insert(self, element, index):
        if index < 0 and index > self.getCount():
            raise ArgumentsRangeError
        if self.is_full():
            return
        # 循环向后移动元素
        for i in range(self.getCount()-1, index-1, -1):
            MyList.__elements[i+1] = MyList.__elements[i]
        # 在index位置，插入元素
        MyList.__elements[index] = element
        # count增加
        self.__count += 1
            
    # 删除某位置的元素
    # index: 表示位置，从1开始
    def delete(self, index):
        if index < 0 and index > self.getCount():
            raise ArgumentsRangeError
        if self.is_empty():
            return False
        try:
                
            # 从index-1 到 count-1位置依次向前移动一个
            while index != self.getCount():
                MyList.__elements[index-1] = MyList.__elements[index]
                index += 1
            # 最后一个元素清零
            MyList.__elements[self.getCount() - 1] = 0
            self.__count -= 1
            return True
        except:
            return False
        
    # 查找某元素的位置
    # element: 要查找的元素
    def search(self, element):
        # 从头遍历元素, 如果查找到element元素，返回元素下标；否则返回-1
        for i in range(0, self.getCount()):
            if element == MyList.__elements[i]:
                return i
        return -1
        
    def toString(self):
        return "maxLength: %d, count: %d" % (self.getMaxLength(), self.getCount())
    
    def getMaxLength(self):
        return self.__max
        
    def getCount(self):
        return self.__count
    
    def getELements(self):
        result = ""
        for ele in MyList.__elements:
            result += "%s " % ele
        return result
    
list1 = MyList(10, 3, [1,3,4])
#print(list1.getELements())
#print(list1.toString())
#print(list1.is_empty())
list1.insert(5,0)
print(list1.getELements())

# 测试删除
list1.delete(2)
print(list1.getELements())

# 测试查找
print(list1.search(5))
print(list1.search(4))
print(list1.search(10))


